package com.cwz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwz.entity.Post;
import com.cwz.entity.User;
import com.cwz.exception.ErrorEnum;
import com.cwz.mapper.UserMapper;
import com.cwz.service.PostService;
import com.cwz.service.UserService;
import com.cwz.util.Result;
import com.cwz.vo.CommentVO;
import com.cwz.entity.Comment;
import com.cwz.vo.PostVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/posts")
@Slf4j
public class PostController {
    
    @Resource
    private PostService postService;

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;
    
    @GetMapping("/list")
    public Result<IPage<PostVO>> getList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpSession session) {
        Page<Post> page = new Page<>(pageNum, pageSize);
        return Result.success(postService.getList(page, getUserId(session)));
    }
    
    @GetMapping("/ranking")
    public Result<Page<Post>> getRankingList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Post> page = new Page<>(pageNum, pageSize);
        return Result.success(postService.getRankingList(page));
    }
    
    @PostMapping("/like/{postId}")
    public Result<Boolean> toggleLike(@PathVariable Long postId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        log.info("点赞用户id:{}",userId);
        return Result.success(postService.toggleLike(postId, userId));
    }
    
    @PostMapping("/comment")
    public Result<Boolean> addComment(@RequestBody Comment comment,HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        comment.setUserId(userId);
        return Result.success(postService.addComment(comment));
    }
    
    @GetMapping("/comments/{postId}")
    public Result<IPage<CommentVO>> getCommentList(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        return Result.success(postService.getCommentList(page, postId));
    }


    //admin
    @PostMapping("/admin/add")
    public Result<Boolean> addPost(@RequestBody Post post,HttpSession session) {
        String username = userMapper.selectById(getUserId(session)).getUsername();
        log.info("用户名:{}",username);
        post.setUsername(username);
        post.setUserId(getUserId(session));
        return Result.success(postService.save(post));
    }
    
    @PutMapping("/admin/update")
    public Result<Boolean> updatePost(@RequestBody Post post) {
        return Result.success(postService.updateById(post));
    }
    
    @DeleteMapping("/admin/delete/{id}")
    public Result<Boolean> deletePost(@PathVariable Long id) {
        return Result.success(postService.removeById(id));
    }
    
    @GetMapping("/admin/stats")
    public Result<Map<String, Object>> getStats() {
        return Result.success(postService.getStats());
    }
    
    @DeleteMapping("/admin/comment/{id}")
    public Result<Boolean> deleteComment(@PathVariable Long id) {
        return Result.success(postService.deleteComment(id));
    }

    public static Integer getUserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        return user.getId();
    }
} 